package cn.iocoder.yudao.module.blog.service.manager;

import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.manager.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.manager.BlogManagerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.manager.BlogManagerConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.manager.BlogManagerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 论坛管理员名单 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogManagerServiceImpl implements BlogManagerService {

    @Resource
    private BlogManagerMapper managerMapper;

    @Resource
    private MemberUserService memberUserService;

    @Override
    public Long createManager(BlogManagerCreateReqVO createReqVO) {
        // 插入
        BlogManagerDO manager = BlogManagerConvert.INSTANCE.convert(createReqVO);
        managerMapper.insert(manager);
        // 返回
        return manager.getId();
    }

    @Override
    public void updateManager(BlogManagerUpdateReqVO updateReqVO) {
        // 校验存在
        validateManagerExists(updateReqVO.getId());
        // 更新
        BlogManagerDO updateObj = BlogManagerConvert.INSTANCE.convert(updateReqVO);
        managerMapper.updateById(updateObj);
    }

    @Override
    public void deleteManager(Long id) {
        // 校验存在
        validateManagerExists(id);
        // 删除
        managerMapper.deleteById(id);
    }

    private void validateManagerExists(Long id) {
        if (managerMapper.selectById(id) == null) {
            throw exception(MANAGER_NOT_EXISTS);
        }
    }

    @Override
    public BlogManagerDO getManager(Long id) {
        return managerMapper.selectById(id);
    }

    @Override
    public List<BlogManagerDO> getManagerList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return managerMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogManagerDO> getManagerPage(BlogManagerPageReqVO pageReqVO) {
        return managerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogManagerDO> getManagerList(BlogManagerExportReqVO exportReqVO) {
        return managerMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean isManager(Long memberId){
        MemberUserDO memberUserDO = memberUserService.getUser(memberId);
        if(memberUserDO == null){
            return false;
        }

        BlogManagerDO managerDO = managerMapper.selectOne(BlogManagerDO::getMobile, memberUserDO.getMobile());
        if(managerDO == null){
            return false;
        }

        return true;
    }

}
